package hibernate.roomSystem;

import hibernate.billStream.ConcreteBill;

import java.util.*;
import hibernate.billStream.*;
import hibernate.clientAccount.*;

public class RoomState {

	public static class ConcreteType{
		
		private String name;
		
		private ConcreteType(String n){
			name=n;
		}
		
		public String toString(){
			return name;
		}
		
		public static ConcreteType reserved=new ConcreteType("RESERVED");
		public static ConcreteType empty=new ConcreteType("EMPTY");
		public static ConcreteType inRepair=new ConcreteType("IN_REPAIR");
		public static ConcreteType dirty=new ConcreteType("DIRTY");
		public static ConcreteType forSelfUse=new ConcreteType("FOR_SELF_USE");
		public static ConcreteType occupied=new ConcreteType("OCCUPIED");
		
		public static ConcreteType getType(String str){
			if(str.equals(reserved.name))
				return reserved;
			else if(str.equals(empty.name))
				return empty;
			else if(str.equals(inRepair.name))
				return inRepair;
			else if(str.equals(dirty.name))
				return dirty;
			else if(str.equals(forSelfUse.name))
				return forSelfUse;
			else if(str.equals(occupied.name))
				return occupied;
			throw new IllegalArgumentException("no such type");
		}
		
		//for debug only
		public static ConcreteType getRandomType(){
			int num=new Random().nextInt(6);
			switch(num){
			case 0:
				return reserved;
			case 1:
				return empty;
			case 2:
				return inRepair;
			case 3:
				return dirty;
			case 4:
				return forSelfUse;
			default:
				return occupied;
			}
		}
	}
	
	private SingleClient currentClient;
	private ConcreteBill currentBill;
	private Set states=new HashSet();
	private Long id;
	private Room room;
	
	public Room getRoom(){return room;}
	
	public void setRoom(Room room){this.room=room;}
	
	public Long getId(){return id;}
	
	public void setId(Long id){this.id=id;}
	
	public void setCurrentClient(SingleClient client){
		currentClient=client;
	}
	
	public SingleClient getCurrentClient(){
		return currentClient;
	}
	
	public void setCurrentBill(ConcreteBill bill){
		currentBill=bill;
	}
	
	public ConcreteBill getCurrentBill(){
		return currentBill;
	}
	
	public void setStates(String states){
		this.states=changeAndSet(states);
	}
	
	public String getStates(){
		StringBuffer ret=new StringBuffer();
		Iterator itr=this.states.iterator();
		while(itr.hasNext())
			ret.append(itr.next().toString()+"|");
		ret.deleteCharAt(ret.length()-1);
		return ret.toString();
	}
	
	private Set changeAndSet(String set){
		StringTokenizer token=new StringTokenizer(set,"|");
		Set cset = new HashSet();
		
		while(token.hasMoreTokens()){
			String str=token.nextToken();
			if(str.equals("RESERVED"))
				cset.add(ConcreteType.reserved);
			else if(str.equals("EMPTY"))
				cset.add(ConcreteType.empty);
			else if(str.equals("IN_REPAIR"))
				cset.add(ConcreteType.inRepair);
			else if(str.equals("DIRTY"))
				cset.add(ConcreteType.dirty);
			else if(str.equals("FOR_SELF_USE"))
				cset.add(ConcreteType.forSelfUse);
			else if(str.equals("OCCUPIED"))
				cset.add(ConcreteType.occupied);
		}
		
		return cset;
		
	}
	
	public boolean equals(Object obj){
		if(!(obj instanceof RoomState))
			return false;
		else 
			return this.id.equals(((RoomState)obj).id);
	}
	
}
